//
//  LoginViewController.m
//  QuickCure
//
//  Created by haoge on 16/9/10.
//  Copyright © 2016年 haoge. All rights reserved.
//
/*
 
 登录的界面:UI布局->1.快医图片2.输入框3.登录按钮
 
 */
#import "LoginViewController.h"
#import <Masonry.h>
#import <SVProgressHUD.h>
@interface LoginViewController ()<UITextFieldDelegate>

@property (weak,nonatomic)UIImageView *addImageView;

@property (weak,nonatomic)UIImageView *doctorImageView;

@property (weak,nonatomic)UITextField *accTextFiled;

@property (weak,nonatomic)UITextField *passTextFiled;

@property (nonatomic, assign) CGFloat maxY;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self creatDoctorPictureView];
    
    [self creatLoginText];
    
    [self creatLoginButton];
    
    self.navigationItem.title = @"登录";
    
//    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
//    [center addObserver:self selector:@selector(changeKetBoard:) name:UIKeyboardWillShowNotification object:nil];
//    [center addObserver:self selector:@selector(changeKetBoard:) name:UIKeyboardWillHideNotification object:nil];
    
    _accTextFiled.delegate = self;
    _passTextFiled.delegate = self;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillChanged:) name:UIKeyboardWillChangeFrameNotification object:nil];
}


#pragma mark -- 快医布局

- (void)creatDoctorPictureView{
    
    UIImageView *picture = [[UIImageView alloc]init];
    
    picture.image = [UIImage imageNamed:@"product_img_logo_nor.png"];
    
    [picture sizeToFit];
    
    self.addImageView = picture;
    
    [self.view addSubview:picture];
    
    [picture mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).offset(150);
        
    }];
    
    UIImageView *pictureDoctor = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"product_text_logo_nor.png"]];
    
    [pictureDoctor sizeToFit];
    
    self.doctorImageView = pictureDoctor;
    
    [self.view addSubview:pictureDoctor];
    
    [pictureDoctor mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(picture);
        make.top.equalTo(picture.mas_bottom);
    }];
    
}

#pragma mark -- 设置输入框

- (void)creatLoginText{
    
    UITextField *accountText = [[UITextField alloc]init];
    
    accountText.placeholder = @"请输入用户名";

    accountText.clearButtonMode = UITextFieldViewModeAlways;
    
    accountText.borderStyle = UITextBorderStyleRoundedRect;
    
    [accountText setAutocorrectionType:UITextAutocorrectionTypeNo];
    
    self.accTextFiled = accountText;
    
    [self.view addSubview:accountText];
    
    [accountText mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.doctorImageView.mas_bottom).offset(50);
        make.leading.equalTo(self.view).offset(30);
        make.trailing.equalTo(self.view).offset(-30);
    }];
    
    UITextField *passwordText = [[UITextField alloc]init];
    
    passwordText.placeholder = @"请输入登录密码";
    
    [passwordText setAutocorrectionType:UITextAutocorrectionTypeNo];
    
    passwordText.borderStyle = UITextBorderStyleRoundedRect;
    
    passwordText.clearButtonMode = UITextFieldViewModeAlways;
    
    passwordText.secureTextEntry = YES;
    
    self.passTextFiled = passwordText;
    
    [self.view addSubview:passwordText];
    
    [passwordText mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(accountText);
        make.top.equalTo(accountText.mas_bottom).offset(30);
        make.leading.equalTo(self.view).offset(30);
        make.trailing.equalTo(self.view).offset(-30);
    }];
    
}

//- (void)viewWillAppear:(BOOL)animated{
//    
//    [super viewWillAppear:animated];
//    
//    [self.accTextFiled becomeFirstResponder];
//}

#pragma mark -- 设置登录按钮

- (void)creatLoginButton{
    
    UIButton *loginBtn = [[UIButton alloc]init];
        
    [loginBtn setBackgroundImage:[UIImage imageNamed:@"link_button_01"] forState:UIControlStateNormal];
    
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    
    [loginBtn setTintColor:[UIColor whiteColor]];
    
    [self.view addSubview:loginBtn];
    
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.passTextFiled);
        make.top.equalTo(self.passTextFiled.mas_bottom).offset(30);
        make.leading.equalTo(self.view).offset(30);
        make.trailing.equalTo(self.view).offset(-30);
        
    }];
    
    [loginBtn addTarget:self action:@selector(clickLoginButton) forControlEvents:UIControlEventTouchUpInside];
}

- (void)clickLoginButton{

    
    if([_accTextFiled.text isEqualToString:_passTextFiled.text]){
    
        [self.navigationController popViewControllerAnimated:YES];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"a" object:nil];
        
    }
    else{
    
        [SVProgressHUD showErrorWithStatus:@"用户名或密码错误,请重新登录"];
        
        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
        
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeGradient];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [SVProgressHUD dismiss];
            
        });
    
    }
    
}

- (void)keyBoardWillChanged:(NSNotification *)noti {
    
    NSDictionary *dict = noti.userInfo;
    
    CGFloat duration = [dict[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];
    
    CGFloat keyBoardMinY = [dict[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue].origin.y;
    
    [UIView animateWithDuration:duration animations:^{
        
        if (_maxY > keyBoardMinY) {
            
            self.view.transform = CGAffineTransformMakeTranslation(0, keyBoardMinY - _maxY - 10);
        } else {
            self.view.transform = CGAffineTransformIdentity;
        }
    }];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.view endEditing:YES];
}


#pragma mark
#pragma mark - 在代理方法中, 可以获取到  被点击的textField 对象
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    
    _maxY = CGRectGetMaxY(textField.frame);
}


//- (void)changeKetBoard:(NSNotification *)notification{
    
    /*
     NSConcreteNotification 0x7f814b925c80
     {         name = UIKeyboardWillShowNotification; userInfo = {
     //        UIKeyboardAnimationCurveUserInfoKey = 7;   执行动画的方式,执行动画的样式
     //        UIKeyboardAnimationDurationUserInfoKey = "0.25";  动画执行的时间
     //        UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}"; 键盘的bounds
     //        UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 796}"; 键盘开始(为显示)的中心点
     //        UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 538}";  键盘结束(已经显示)的中心店
     //        UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 258}}"; 键盘未显示时的frame
     //        UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 409}, {375, 258}}"; 键盘结束时的frame
     //        UIKeyboardIsLocalUserInfoKey = 1; 是否是当前弹出的键盘
     }
     */
    
//    NSDictionary *userInfo = notification.userInfo;
//    
//    CGRect endFrame = [userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
//    
//    CGFloat move = endFrame.origin.y - self.view.bounds.size.height;
//    
//    self.view.transform = CGAffineTransformMakeTranslation(0, move);
//}

@end
